﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Crm.CrmClient;

namespace Crm.Views
{
    public partial class DashBoard : Page
    {
        public bool AdayCari { get; set; }
        public string CariKodu { get; set; }
        public string CariUnvani { get; set; }
        public int CariID { get; set; }
        public Hareket Hr { get; set; }
        CrmClient.CrmServiceClient client = new CrmClient.CrmServiceClient();

        public DashBoard(bool aday, string carikodu, string unvani, int cariID, Hareket hr)
        {
            InitializeComponent();
            AdayCari = aday;
            CariKodu = carikodu;
            CariUnvani = unvani;
            CariID = cariID;
            Hr = hr;
            client.GenelKampanyalarCompleted += client_GenelKampanyalarCompleted;
            client.FirmaKampanyaCompleted += client_FirmaKampanyaCompleted;
            client.CariKategoriRaporuCompleted += client_CariKategoriRaporuCompleted;
            client.CariSatisRaporuCompleted += client_CariSatisRaporuCompleted;
            client.FirmayaAitSonHareketCompleted += client_FirmayaAitSonHareketCompleted;
            client.CariBakiyeRaporuCompleted += client_CariBakiyeRaporuCompleted;
            client.KategoriDetayRaporuCompleted += client_KategoriDetayRaporuCompleted;
            grdAylikSatislar.RowClick += grdAylikSatislar_RowClick;
            btnRandevu.Click += btnRandevu_Click;
            this.Loaded += DashBoard_Loaded;
            client.RandevuyaBaslaCompleted += client_RandevuyaBaslaCompleted;
        }

        void client_RandevuyaBaslaCompleted(object sender, RandevuyaBaslaCompletedEventArgs e)
        {
            if (e.Error == null && e.Result != null)
            {
                Gorusme gr = new Gorusme();
                gr.Hareket = e.Result;
                gr.EskiEvrak = true;
                gr.GorusmeEvragi = false;
                gr.Show();
            }
        }

        void btnRandevu_Click(object sender, RoutedEventArgs e)
        {
            Kullanici usr = Session.Instance.GetValue<Kullanici>("user");
            AdayCari = AdayCari;
            CariKodu = CariKodu;
            CariUnvani = CariUnvani;
            CariID = CariID;
            client.RandevuyaBaslaAsync(AdayCari, CariID, CariKodu, CariUnvani, usr.ID);
        }

        void grdAylikSatislar_RowClick(object sender, DevExpress.AgDataGrid.RowClickEventArgs e)
        {
            if (grdAylikSatislar.FocusedRowHandle > -1)
            {
                string kategorikodu = grdAylikSatislar.GetCellValue(grdAylikSatislar.FocusedRowHandle, grdAylikSatislar.Columns["Kod"]).ToString();
                string ayadi = grdAylikSatislar.GetCellValue(grdAylikSatislar.FocusedRowHandle, grdAylikSatislar.Columns["Ay"]).ToString();

                client.KategoriDetayRaporuAsync(CariKodu, kategorikodu, getAy(ayadi));
            }
        }

        private int getAy(string ayAdi)
        {
            int sonuc = 0;
            switch (ayAdi)
            {
                case "OCAK":
                    sonuc = 1;
                    break;
                case "ŞUBAT":
                    sonuc = 2;
                    break;
                case "MART":
                    sonuc = 3;
                    break;
                case "NİSAN":
                    sonuc = 4;
                    break;
                case "MAYIS":
                    sonuc = 5;
                    break;
                case "HAZİRAN":
                    sonuc = 6;
                    break;
                case "TEMMUZ":
                    sonuc = 7;
                    break;
                case "AĞUSTOS":
                    sonuc = 8;
                    break;
                case "EYLÜL":
                    sonuc = 9;
                    break;
                case "EKİM":
                    sonuc = 10;
                    break;
                case "KASIM":
                    sonuc = 11;
                    break;
                case "ARALIK":
                    sonuc = 12;
                    break;
            }
            return sonuc;
        }

        void client_KategoriDetayRaporuCompleted(object sender, KategoriDetayRaporuCompletedEventArgs e)
        {
            if (e.Error == null && e.Result != null)
            {
                grdSatisDetay.DataSource = e.Result;
                grdSatisDetay.Columns["UrunAdi"].VisibleIndex = 0;
                grdSatisDetay.Columns["Miktar"].VisibleIndex = 1;
                grdSatisDetay.Columns["Tutar"].VisibleIndex = 2;
                grdOdeme.ColumnsAutoWidth = true;
            }
        }

        void client_CariBakiyeRaporuCompleted(object sender, CariBakiyeRaporuCompletedEventArgs e)
        {
            if (e.Error == null && e.Result != null)
            {
                grdOdeme.DataSource = e.Result;
                grdOdeme.Columns["SorumlulukMerkezi"].VisibleIndex = 0;
                grdOdeme.Columns["Bakiye"].VisibleIndex = 1;
                grdOdeme.ColumnsAutoWidth = true;
            }
        }

        void client_FirmayaAitSonHareketCompleted(object sender, FirmayaAitSonHareketCompletedEventArgs e)
        {
            if (e.Error == null && e.Result != null)
            {
                grdGorusmeRandevu.DataSource = e.Result;
                grdGorusmeRandevu.Columns["Tip"].VisibleIndex = 0;
                grdGorusmeRandevu.Columns["Tarih"].VisibleIndex = 1;
                grdGorusmeRandevu.Columns["Konu"].VisibleIndex = 2;
                grdGorusmeRandevu.Columns["Yer"].VisibleIndex = 3;
                grdGorusmeRandevu.ColumnsAutoWidth = true;
            }
        }

        void client_CariSatisRaporuCompleted(object sender, CariSatisRaporuCompletedEventArgs e)
        {
            if (e.Error == null && e.Result != null)
            {
                grdEncokSatis.DataSource = e.Result;
                grdEncokSatis.Columns["UrunAdi"].VisibleIndex = 0;
                grdEncokSatis.Columns["UrunAdi"].Width = 250;
                grdEncokSatis.Columns["Miktar"].VisibleIndex = 1;
                grdEncokSatis.Columns["Miktar"].Width = 50;
                grdEncokSatis.Columns["Tutar"].VisibleIndex = 2;
                grdEncokSatis.Columns["Tutar"].Width = 50;
            }
        }

        void client_CariKategoriRaporuCompleted(object sender, CariKategoriRaporuCompletedEventArgs e)
        {
            if (e.Error == null && e.Result != null)
            {
                grdAylikSatislar.DataSource = e.Result.OrderBy(x => x.Ay).OrderBy(x => x.Kategori);
                if (e.Result.Count > 0)
                {
                    grdAylikSatislar.Columns["Ay"].VisibleIndex = 0;
                    grdAylikSatislar.Columns["Kategori"].VisibleIndex = 1;
                    grdAylikSatislar.Columns["Ciro"].VisibleIndex = 2;
                    grdAylikSatislar.Columns["Kod"].Visible = false;
                    grdAylikSatislar.ColumnsAutoWidth = true;
                }
            }
        }

        void DashBoard_Loaded(object sender, RoutedEventArgs e)
        {
            client.GenelKampanyalarAsync();
            client.FirmaKampanyaAsync(CariKodu);
            client.CariKategoriRaporuAsync(CariKodu);
            client.CariSatisRaporuAsync(CariKodu);
            client.FirmayaAitSonHareketAsync(CariKodu);
            client.CariBakiyeRaporuAsync(CariKodu);
        }

        void client_FirmaKampanyaCompleted(object sender, FirmaKampanyaCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                grdFirmaKampanya.DataSource = e.Result;
                grdFirmaKampanya.Columns["No"].VisibleIndex = 0;
                grdFirmaKampanya.Columns["KampanyaAdi"].VisibleIndex = 1;
                grdFirmaKampanya.Columns["KampanyaAdi"].HeaderContent = "Kampanya Adı";
                grdFirmaKampanya.Columns["StokAdi"].VisibleIndex = 2;
                grdFirmaKampanya.Columns["StokAdi"].HeaderContent = "Stok Adı";
                grdFirmaKampanya.Columns["Fiyat"].VisibleIndex = 3;
            }
        }

        void client_GenelKampanyalarCompleted(object sender, GenelKampanyalarCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                grdGenelKampanya.DataSource = e.Result;
                grdGenelKampanya.Columns["No"].VisibleIndex = 0;
                grdGenelKampanya.Columns["KampanyaAdi"].VisibleIndex = 1;
                grdGenelKampanya.Columns["KampanyaAdi"].HeaderContent = "Kampanya Adı";
                grdGenelKampanya.Columns["StokAdi"].VisibleIndex = 2;
                grdGenelKampanya.Columns["StokAdi"].HeaderContent = "Stok Adı";
                grdGenelKampanya.Columns["Fiyat"].VisibleIndex = 3;
            }
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }


        private void btnGorusme_Click(object sender, RoutedEventArgs e)
        {
            if (Hr != null)
            {
                Gorusme gr = new Gorusme();
                gr.EskiEvrak = true;
                gr.Hareket = Hr;
                gr.IsDashBoard = true;
                gr.Closed += gr_Closed;
                gr.Show();
            }
        }

        void gr_Closed(object sender, EventArgs e)
        {
            Survey frm = new Survey();
            frm.MusteriID = CariID;
            frm.Closed += frm_Closed;
            frm.Show();
        }

        void frm_Closed(object sender, EventArgs e)
        {
            this.LayoutRoot.RowDefinitions.Clear();
            this.LayoutRoot.ColumnDefinitions.Clear();
            this.LayoutRoot.Children.Clear();
            this.LayoutRoot.Children.Add(new MainPage());
        }

        private void btnCikis_Click(object sender, RoutedEventArgs e)
        {
            this.LayoutRoot.RowDefinitions.Clear();
            this.LayoutRoot.ColumnDefinitions.Clear();
            this.LayoutRoot.Children.Clear();
            this.LayoutRoot.Children.Add(new MainPage());
        }

    }
}
